package activity.video.adapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.hichip.campro.R;
import com.hichip.content.HiChipDefines;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class VideoOnlineListAdapter extends BaseAdapter {
    private List<HiChipDefines.HI_P2P_FILE_INFO> file_list = Collections.synchronizedList(new ArrayList<HiChipDefines.HI_P2P_FILE_INFO>());
    private LayoutInflater mInflater;
    private Context context;
    private SimpleDateFormat sdf = new SimpleDateFormat("00:mm:ss");

    public VideoOnlineListAdapter(Context context,List<HiChipDefines.HI_P2P_FILE_INFO> file_list) {
        this.mInflater = LayoutInflater.from(context);
        this.file_list=file_list;
        this.context=context;
    }

    public int getCount() {
        return file_list.size();
    }

    public Object getItem(int position) {
        return file_list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean isEnabled(int position) {

        if (file_list.size() == 0)
            return false;
        return super.isEnabled(position);
    }

    @SuppressLint("InflateParams")
    public View getView(int position, View convertView, ViewGroup parent) {

        final HiChipDefines.HI_P2P_FILE_INFO evt = (HiChipDefines.HI_P2P_FILE_INFO) getItem(position);
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_video_online, null);
            holder = new ViewHolder();
            holder.event = (TextView) convertView.findViewById(R.id.txt_event);
            holder.time = (TextView) convertView.findViewById(R.id.txt_time);
            holder.fileSize = (TextView) convertView.findViewById(R.id.txt_file_size);
            holder.tvLong = (TextView) convertView.findViewById(R.id.txt_long);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final String[] mTextArray = context.getResources().getStringArray(R.array.online_recording_type);

        if (evt.EventType <= 3 && evt.EventType >= 0) {
            holder.event.setText(mTextArray[evt.EventType]);
            switch (evt.EventType) {
                case 1:
                    holder.event.setTextColor(context.getResources().getColor(R.color.color_connecting));
                    break;
                case 2:// 报警录像
                    holder.event.setTextColor(context.getResources().getColor(R.color.color_pass_word));
                    break;
                case 3:// 计划录像
                    holder.event.setTextColor(context.getResources().getColor(R.color.color_login));
                    break;
            }
        } else {
            holder.event.setText("");
        }
        String string1 = evt.sEndTime.toString();
        String[] strings = string1.split(" ");
        String string2 = evt.sStartTime.toString();
        String[] strings2 = string2.split(" ");
        holder.time.setText(strings2[0] + "        " + strings2[1] + " - " + strings[1]);
        if (evt.u32size < 1) {
            holder.fileSize.setText("1 MB");
        } else {
            holder.fileSize.setText(evt.u32size + " MB");
        }
        long duration = evt.sEndTime.getTimeInMillis() - evt.sStartTime.getTimeInMillis();
        String string = sdf.format(new java.util.Date(duration));
        holder.tvLong.setText(string);
        return convertView;

    }

    private final class ViewHolder {
        public TextView event;
        public TextView time;
        public TextView fileSize;
        public TextView tvLong;
    }
}
